package com.hnn.config;

import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;

import java.time.Duration;

/**
 * @author hnn
 * @version 1.0
 * @date 2025-05-12 14:33
 */
public class CustomRedisCacheManager extends RedisCacheManager {
    public CustomRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
        super(cacheWriter, defaultCacheConfiguration);
    }

    @Override
    protected RedisCache createRedisCache(String name, RedisCacheConfiguration config) {
        //动态ttl的办法
        String[] parts = name.split("#");
        String cacheName = parts[0];
        if (parts.length > 1) {
            long ttl = Long.parseLong(parts[1]);
            config = config.entryTtl(Duration.ofSeconds(ttl));
        }
        return super.createRedisCache(cacheName, config);
    }
}
